package l2ft.gameserver.model.entity.events.impl.fightclub;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import l2ft.commons.collections.MultiValueSet;
import l2ft.gameserver.ThreadPoolManager;
import l2ft.gameserver.model.Creature;
import l2ft.gameserver.model.Player;
import l2ft.gameserver.model.Skill;
import l2ft.gameserver.model.entity.events.fightclubmanager.FightClubPlayer;
import l2ft.gameserver.model.entity.events.fightclubmanager.FightClubLastStatsManager.FightClubStatType;
import l2ft.gameserver.model.entity.events.impl.AbstractFightClub;
import l2ft.gameserver.model.instances.NpcInstance;

public class FFATreasureHuntEvent extends AbstractFightClub
{
	private static final int CHEST_ID = 40023;
	private final double _badgesOpenChest;
	private final int _scoreForKilledPlayer;
	private final int _scoreForChest;
	private final int _timeForRespawningChest;
	private final int _numberOfChests;
	private List<NpcInstance> _spawnedChests = new CopyOnWriteArrayList<>();
	
	
	public FFATreasureHuntEvent(MultiValueSet<String> set)
	{
		super(set);
		_badgesOpenChest = set.getDouble("badgesOpenChest");
		_scoreForKilledPlayer = set.getInteger("scoreForKilledPlayer");
		_scoreForChest = set.getInteger("scoreForChest");
		_timeForRespawningChest = set.getInteger("timeForRespawningChest");
		_numberOfChests = set.getInteger("numberOfChests");
	}
	
	@Override
	public void onKilled(Creature actor, Creature victim, Skill skill)
	{
		if(actor != null && actor.isPlayable())
		{
			FightClubPlayer realActor = getFightClubPlayer(actor.getPlayer());
			if(victim.isPlayer())
			{
				realActor.increaseKills(true);
				realActor.increaseScore(_scoreForKilledPlayer);
				updatePlayerScore(realActor);
				sendMessageToPlayer(realActor, MESSAGE_TYPES.HERO, "You have killed "+victim.getName());
			}
			else if(victim.isPet())
			{
				
			}
			actor.getPlayer().sendUserInfo();
		}
		
		if(victim.isPlayer())
		{
			FightClubPlayer realVictim = getFightClubPlayer(victim);
			realVictim.increaseDeaths();
			if(actor != null)
				sendMessageToPlayer(realVictim, MESSAGE_TYPES.HERO, "You have been killed by "+actor.getName());
			victim.getPlayer().sendUserInfo();
		}
		
		super.onKilled(actor, victim, skill);
	}
	
	private void spawnChest()
	{
		_spawnedChests.add(chooseLocAndSpawnNpc(CHEST_ID, getMap().getKeyLocations(), 0));
	}
	
	@Override
	public void startRound()
	{
		super.startRound();
		
		for(int i = 0;i<_numberOfChests;i++)
			spawnChest();
	}
	
	@Override
	public void stopEvent()
	{
		super.stopEvent();
		
		for(NpcInstance chest : _spawnedChests)
			if(chest != null && !chest.isDead())
				chest.deleteMe();
		_spawnedChests.clear();
	}
	
	/**
	 * @return should it disappear?
	 */
	public boolean openTreasure(Player player, NpcInstance npc)
	{
		FightClubPlayer fPlayer = getFightClubPlayer(player);
		if(fPlayer == null)
			return false;
		if(getState() != EVENT_STATE.STARTED)
			return false;
		
		fPlayer.increaseEventSpecificScore("chest");
		fPlayer.increaseScore(_scoreForChest);
		updatePlayerScore(fPlayer);
		
		ThreadPoolManager.getInstance().schedule(new Runnable() {
			
			@Override
			public void run()
			{
				if(getState() != EVENT_STATE.NOT_ACTIVE)
					spawnChest();
			}
		}, _timeForRespawningChest*1000);
		
		_spawnedChests.remove(npc);
		
		return true;
	}

	@Override
	protected int getBadgesEarned(FightClubPlayer fPlayer, int currentValue)
	{
		currentValue += addMultipleBadgeToPlayer(fPlayer, FightClubStatType.OPEN_CHEST, fPlayer.getEventSpecificScore("chest"), _badgesOpenChest, fPlayer.getSecondsSpentOnEvent());
		return super.getBadgesEarned(fPlayer, currentValue);
	}
	
	@Override
	public String getVisibleTitle(Player player, String currentTitle, boolean toMe)
	{
		FightClubPlayer fPlayer = getFightClubPlayer(player);
		
		if(fPlayer == null)
			return currentTitle;
		
		return "Chests: " + fPlayer.getEventSpecificScore("chest") + " Kills: "+fPlayer.getKills(true);
	}
}
